﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ScytheEngine
{
	public enum PositionAlignment
	{
		Center,
		LeftAlign,
		RightAlign,
		TopCenter,
		TopLeftAlign,
		TopRightAlign,
		BottomCenter,
		BottomLeftAlign,
		BottomRightAlign
	}

	public static class Utilities2D
	{
		/// <summary>
		/// Generates a 2D position that is proportionately to the viewport
		/// </summary>
		/// <param name="viewport_width">The current viewport width</param>
		/// <param name="viewport_height">The current viewport height</param>
		/// <param name="horizontal_pos">The proportionate horizontal position (or the zero based index of the vertical slice)</param>
		/// <param name="horizontal_subdivs">The total number of horizontal subdivisions (or vertical slices)</param>
		/// <param name="vertical_pos">The proportionate vertical position (or the zero based index of the horizontal slice)</param>
		/// <param name="vertical_subdivs">The total number of vertical subdivisions (or horizontal slices)</param>
		/// <param name="aligment">The alignment of the position within the given subdivision</param>
		/// <returns></returns>
		/// <example>GenerateViewportProportionatePosition(viewport, 1, 1, 3, 3, PositionAlignment.Center) would generate a position at the center of the viewport</example>
		public static Vector2 GenerateViewportProportionatePosition(
			int viewport_width, int viewport_height,
			int horizontal_pos, int horizontal_subdivs, int vertical_pos, int vertical_subdivs, 
			PositionAlignment aligment)
		{
			int x = horizontal_pos <= 0 ? horizontal_pos : (viewport_width / horizontal_subdivs) * horizontal_pos;
			int y = vertical_pos <= 0 ? vertical_pos : (viewport_height / vertical_subdivs) * vertical_pos;

			Vector2 horizontal_offset = new Vector2( viewport_width / horizontal_subdivs / 2, 0 );
			Vector2 vertical_offset = new Vector2( 0, viewport_height / vertical_subdivs / 2 );
			Vector2 diagonal_offset = new Vector2( viewport_width / horizontal_subdivs / 2, viewport_height / vertical_subdivs / 2 );
		
			Vector2 center_pos = new Vector2( x, y ) + diagonal_offset;
			
			Vector2 offset;

			switch(aligment)
			{
				case PositionAlignment.TopLeftAlign:	offset.X = -diagonal_offset.X; offset.Y = -diagonal_offset.Y; break;
				case PositionAlignment.TopCenter:		offset = vertical_offset; break;
				case PositionAlignment.TopRightAlign:	offset = diagonal_offset; break;
				case PositionAlignment.LeftAlign:		offset = -horizontal_offset; break;
				case PositionAlignment.RightAlign:		offset = horizontal_offset; break;
				case PositionAlignment.BottomLeftAlign: offset = -diagonal_offset; break;
				case PositionAlignment.BottomCenter:	offset = -vertical_offset; break;
				case PositionAlignment.BottomRightAlign: offset.X = diagonal_offset.X; offset.Y = -diagonal_offset.Y; break;
				default: offset = Vector2.Zero; break;
			}

			return center_pos + offset;
		}
	}
}
